package routes

import (
	"GoWeb_Advanced/00_bluebell/controllers"
	"GoWeb_Advanced/00_bluebell/logger"
	"GoWeb_Advanced/00_bluebell/middlewares"
	"net/http"
	"time"

	swaggerFiles "github.com/swaggo/files"

	_ "GoWeb_Advanced/00_bluebell/docs" //千万不要忘了导入上一步生成的 docs

	"github.com/gin-gonic/gin"
	gs "github.com/swaggo/gin-swagger"

	"github.com/gin-contrib/pprof"
)

func Setup(mode string) *gin.Engine {
	if mode == gin.ReleaseMode { //当我们设置的模式等于 gin.ReleaseMode 模式时
		gin.SetMode(gin.ReleaseMode) //gin 设置成发布模式，其他情况下默认为调试模式，会在终端打印[gin-debug]信息
	}
	r := gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.RateLimitMiddleware(2*time.Second, 1))

	r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))

	v1 := r.Group("api/v1")

	//注册业务路由
	v1.POST("signUp", controllers.SignUpHandler)
	//登录业务路由
	v1.POST("login", controllers.LoginHandler)

	v1.Use(middlewares.JWTAuthMiddleware()) //应用 JWT 认证中间件

	{
		v1.GET("/community", controllers.CommunityHandler)
		v1.GET("/community/:id", controllers.CommunityDetailHandler)

		v1.POST("/post", controllers.CreatePostHandler)
		v1.GET("/post/:id", controllers.GetPostDetailHandler)
		v1.GET("/posts", controllers.GetPostListHandler)
		//跟据时间或分数获取帖子列表
		v1.GET("/posts2", controllers.GetPostListHandler2)
		//投票
		v1.POST("/vote", controllers.PostVoteHandler)
	}

	r.GET("/ping", middlewares.JWTAuthMiddleware(), func(context *gin.Context) {
		//如果是登录的用户，判断请求头中是否有 有效的 JWT
		context.String(http.StatusOK, "xxx")
	})

	pprof.Register(r) //注册 pprof相关路由
	r.Run()

	return r
}
